package com.qcq.wecheck.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.qcq.wecheck.entity.Schedule;
import com.qcq.wecheck.entity.User;
import com.qcq.wecheck.mapper.ScheduleMapper;
import com.qcq.wecheck.service.ScheduleService;
import com.qcq.wecheck.util.OssUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 签到表 服务实现类
 * </p>
 *
 * @author O
 * @since 2018-09-19
 */
@Service
public class ScheduleServiceImpl extends ServiceImpl<ScheduleMapper, Schedule> implements ScheduleService {
	@Resource
	private ScheduleMapper scheduleMapper;

	@Override
	public List<Schedule> getEngaged(String openId, Integer pageNo, Integer pageSize) {
		return scheduleMapper.selectEngaged(openId, pageNo, pageSize);
	}

	@Override
	public List<User> getMember(Long id, String date) {
		return scheduleMapper.getMember(id, date);
	}

	@Override
	public Schedule selectByKeyword(String keyword) {
		return scheduleMapper.selectByKeyword(keyword);
	}

	@Override
	public boolean hasKeyword(Schedule schedule) {
		if (schedule.getKeyword() != null) {
			return scheduleMapper.selectByKeyword(schedule.getKeyword()) != null;
		}
		return false;
	}

	@Override
	public boolean addOne(Schedule schedule) {
		schedule.setCreateTime(LocalDateTime.now());
		schedule.setImgUrl(OssUtils.getRandomUrl());
		return scheduleMapper.insert(schedule) > 0;
	}

	@Override
	public List<Schedule> getOwn(String openId, Integer pageNo, Integer pageSize) {
		Page<Schedule> page = new Page<>(pageNo, pageSize);
		EntityWrapper<Schedule> wrapper = new EntityWrapper<>();
		wrapper.eq("creator_id", openId);
		return scheduleMapper.selectPage(page, wrapper);
	}
}
